package com.tigerlee.stopmusic;

import java.util.Calendar;


public class Alarm extends Clock {
	private long mAlarmTime = 0;
	private int mAlarmDay = 0;
	private int mAlarmHour = 0;
	private int mAlarmMinute = 0;
	private boolean mIsAM = true;
	
	private static final String TAG = "Alarm";
	
	public Alarm() {
		super();
	}
	public Alarm(long currentTime){
		super(currentTime);
	}
	
	public int getAlartmDay(){
		return this.mAlarmDay;
	}
	public int getAlarmHour(){
		return this.mAlarmHour;
	}
	public int getAlarmMinute(){
		return this.mAlarmMinute;
	}
	public String getAMPM(){
		return this.mIsAM ? " AM" : " PM";
	}
	public int getAMPMByInt(){
		return this.mIsAM ? 0 : 1;
	}
	
	public void setAMPM(boolean isAM){
		this.mIsAM = isAM;
		setAlarmHour(convertTimeClockTo24Clock(mAlarmHour));
	}
	public void setAlarmDay(int day){
		this.mAlarmDay = day;
	}
	public void setAlarmHour(int hour){
		this.mAlarmHour = hour;
	}
	public void setAlarmMinute(int min){	
		this.mAlarmMinute = min;
	}
	
	public void setAlarmTimeByTimeMills(long time){
		Calendar c = Calendar.getInstance();
		c.setTimeInMillis(time);
		int hour = c.get(Calendar.HOUR_OF_DAY);
		if(hour < 12){
			setAMPM(true);
		}else{
			setAMPM(false);
		}
		setAlarmHour(hour);
		setAlarmMinute(c.get(Calendar.MINUTE));
	}

	public void setDurationMinuteAndChange(int durationMin) {
		// start with now
		setCurrentTime(System.currentTimeMillis());
		long ONE_SECOND = 1000L;
		long ONE_MINUTE = ONE_SECOND * 60L;

		Calendar c = Calendar.getInstance();
		// Minute parameter set to 11 from 0 so plus 1.
		c.setTimeInMillis(getCurrentTime()
				+ (++durationMin * 10) * ONE_MINUTE);

		this.mAlarmHour = c.get(Calendar.HOUR_OF_DAY);
		this.mAlarmMinute = c.get(Calendar.MINUTE);
		this.mIsAM  = this.mAlarmHour >= 12 ? false : true;
	}
	
	public long getAlarmTimeMillis(){
		this.mAlarmTime = calculateAlarmTimeMillis();
		return this.mAlarmTime;
	}

	private long calculateAlarmTimeMillis() {
		// Start from now
		setCurrentTime(System.currentTimeMillis());
		int nowHour = getCurrentHour();
		int nowMinute = getCurrentMinute();

		Calendar c = Calendar.getInstance();
		// if alarm is behind current time, advance one day
		if (this.mAlarmHour < nowHour || this.mAlarmHour == nowHour && this.mAlarmMinute <= nowMinute) {
			c.add(Calendar.DAY_OF_YEAR, 1);
		}
		c.set(Calendar.HOUR_OF_DAY, this.mAlarmHour);
		c.set(Calendar.MINUTE, this.mAlarmMinute);
		c.set(Calendar.SECOND, 0);
		c.set(Calendar.MILLISECOND, 0);

		return c.getTimeInMillis();
	}

	public String[] getRemainTimeToString() {
		return getRemainTimeToString(getAlarmTimeMillis());
	}
	
	public String[] getRemainTimeToString(long alarmTime) {
		long ONE_SECOND = 1000L;
		long ONE_MINUTE = ONE_SECOND * 60L;
		long ONE_HOUR = ONE_MINUTE * 60L;
		// Start from now
		setCurrentTime(System.currentTimeMillis());
		String[] result = new String[3];
		long delta = alarmTime - getCurrentTime();

		// it already passed...
		if (delta < 0) {
			return null;
		}
		long hours = delta / ONE_HOUR; 
		long minutes = delta / ONE_MINUTE % 60;
		long days = hours / 24;
		hours = hours % 24;
		if(minutes == 59){
			hours++;
			minutes=0;
		}else{
			minutes++;
		}
		result[0] = String.format("%02d", (int)hours);
		result[1] = String.format("%02d", (int)minutes);
		result[2] = String.format("%02d", (int)days);

		return result;
	}
	public int convertTimeClockTo24Clock(int hour){
		if(mIsAM){
			if(hour >= 12) hour -= 12;
		}else{
			if(hour < 12) hour += 12;
		}
		return hour;
	}
}
